package elastic

func NewBoolQuery() *Query {
	return &Query{
		Bool: &Bool{},
	}
}

func NewTermQuery(field string, value interface{}) *Query {
	return &Query{
		Term: map[string]interface{}{
			field: value,
		},
	}
}

func NewTermsQuery(field string, value interface{}) *Query {
	return &Query{
		Terms: map[string]interface{}{
			field: value,
		},
	}
}

func NewRangeQuery(field string, start, end int64) *Query {
	return &Query{
		Range: map[string]*Range{
			field: {
				Gte: start,
				Lte: end,
			},
		},
	}
}

func NewMatchAllQuery() *Query {
	return &Query{
		MatchAll: &struct{}{},
	}
}

type Bool struct {
	Must []interface{} `json:"must,omitempty"`
}

type Must struct {
	Term map[string]interface{} `json:"term,omitempty"`
}

type Query struct {
	Bool     *Bool                  `json:"bool,omitempty"`
	Term     map[string]interface{} `json:"term,omitempty"`
	Terms    map[string]interface{} `json:"terms,omitempty"`
	Range    map[string]*Range      `json:"range,omitempty"`
	MatchAll *struct{}              `json:"match_all,omitempty"`
}

type Range struct {
	Gte int64 `json:"gte,omitempty"`
	Lte int64 `json:"lte,omitempty"`
}

func (q *Query) Must(item interface{}) *Query {
	if q.Bool.Must == nil {
		q.Bool.Must = make([]interface{}, 0)
	}
	q.Bool.Must = append(q.Bool.Must, item)
	return q
}
